<template>
	<view class="personal-data">
		<view class="list">
			<view class="item acea-row row-between-wrapper">
				<view>开户行名称</view>
				<view class="input">
					<input type="text" v-model="userInfo.bankName" placeholder="填写开户行名称" />
				</view>
			</view>
			<view class="item acea-row row-between-wrapper">
				<view>银行卡号</view>
				<view class="input">
					<input type="text" v-model="userInfo.bankCode" placeholder="填写银行卡号" />
				</view>
			</view>
		</view>
		<view class="remarks-box">
			<text>1、绑定银行卡后可使用提现功能，信息必须真实有效，否则提现失败</text>
		</view>
		<view class="modifyBnt bg-color-red" @click="submit">保存修改</view>
		<!-- #ifndef MP-WEIXIN -->
		<view class="modifyBnt bg-color-black" @click="BackPage">返回</view>
		<!-- #endif -->
	</view>
</template>
<script>
	import {
		mapGetters
	} from "vuex";
	import {
		trim,
		isWeixin,
		chooseImage
	} from "@/utils";
	import {
		VUE_APP_API_URL
	} from "@/config";
	import {
		postUserEdit,
		getLogout,
		switchH5Login,
		getUserInfo,
	} from "@/api/user";
	import cookie from "@/utils/store/cookie";
	import store from "@//store";
	import dayjs from "dayjs";

	export default {
		name: "PersonalData",
		components: {},
		data: function() {
			return {
				avatar: "",
				isWeixin: false,
				currentAccounts: 0,
				switchUserInfo: [],
				userInfo: {},
				userIndex: 0,
				payPassword: ''
			};
		},
		mounted: function() {
			this.isWeixin = isWeixin();
		},
		onShow() {
			this.getUser();
		},
		methods: {
			BackPage() {
				// #ifdef H5
				const pages = getCurrentPages()
				if (pages.length > 1) {
					uni.navigateBack(1)
					return;
				}
				let a = this.$router.go(-1)
				if (a == undefined) {
					uni.reLaunch({
						url: '/pages/home/index'
					})
				}
				return;
				// #endif
				uni.navigateBack(1)
			},
			goChangePassword() {
				this.$yrouter.push("/pages/user/ChangePassword/index");
			},
			setupPayPwd() {
				this.$yrouter.push("/pages/user/setupPassword/index");
			},
			forgetPayPwd() {
				this.$yrouter.push("/pages/user/forgetPassword/index");
			},
			switchAccounts: function(index) {
				let that = this;
				this.userIndex = index;
				let userInfo = this.switchUserInfo[this.userIndex];
				if (this.switchUserInfo.length <= 1) return true;
				if (userInfo === undefined) {
					uni.showToast({
						title: "切换的账号不存在",
						icon: "none",
						duration: 2000,
					});
					return;
				}
				if (userInfo.user_type === "h5") {
					switchH5Login()
						.then(({
							data
						}) => {
							uni.hideLoading();
							const expires_time = dayjs(data.expires_time);
							store.commit("login", data.token, expires_time);
							that.$emit("changeswitch", false);
							location.reload();
						})
						.catch((err) => {
							uni.hideLoading();
							uni.showToast({
								title: err.msg || err.response.data.msg || err.response.data.message,
								icon: "none",
								duration: 2000,
							});
						});
				} else {
					cookie.set("loginType", "wechat", 60);
					uni.hideLoading();
					this.$store.commit("logout");
					this.$emit("changeswitch", false);
				}
			},
			getUser: function() {
				let that = this;
				getUserInfo().then(res => {
						that.userInfo = res.data
						console.log(that.userInfo)
						that.payPassword = res.data.wechatAccount
						that.avatar = res.data.avatar;
					})
					.catch(error => {
						reject('获取用户信息失败')
					})
			},
			chooseImage() {
				chooseImage((img) => {
					console.log(img);
					this.avatar = img;
				});
			},

			submit: function() {
				let userInfo = this.userInfo;
				let bankCardReg = /^(\d{16}|\d{19})$/;
				// let _IDRe18 = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
				// let _IDre15 =  /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/
				// if( _IDRe18.test( this.userInfo.cardId ) || _IDre15.test( this.userInfo.cardId )  ) {
					
				// }else{
				// 	uni.showToast({
				// 		title: '请输入正确有效的身份证号码',
				// 		icon: "none",
				// 		duration: 2000,
				// 	});
				// 	return
				// }
				if (!bankCardReg.test(this.userInfo.bankCode)) {
					uni.showToast({
						title: '请输入正确有效的银行卡号',
						icon: "none",
						duration: 2000,
					});
					return
				}
				postUserEdit({
					bankCode: trim(this.userInfo.bankCode),
					bankName: trim(this.userInfo.bankName)
				}).then(
					(res) => {
						this.$store.dispatch("userInfo", true);
						uni.showToast({
							title: res.msg,
							icon: "none",
							duration: 2000,
						});
						this.$yrouter.back();
					},
					(err) => {
						uni.showToast({
							title: err.msg || err.response.data.msg || err.response.data.message,
							icon: "none",
							duration: 2000,
						});
					}
				);
			},
			logout: function() {
				uni.showModal({
					title: "提示",
					content: "确认退出登录?",
					success: (res) => {
						if (res.confirm) {
							getLogout()
								.then((res) => {
									this.$store.commit("logout");
									this.$yrouter.replace({
										path: "/pages/user/Login/index",
										query: {},
									});
								})
								.catch((err) => {});
						} else if (res.cancel) {}
					},
				});
			},
		},
	};
</script>
<style lang="less">
	page {
		height: auto;
	}

	.wrapper {
		position: relative;

		.btnSever {
			position: absolute;
			top: 45rpx;
			right: 40rpx;
			height: 70rpx;
			line-height: 70rpx;
			width: 120rpx;
			text-align: center;
			border-radius: 6rpx;
			background-color: rgba(0, 0, 0, 0.7);
			color: #FFFFFF;
			font-size: 30rpx;
		}
	}

	.remarks-box {
		width: 100%;
		padding: 30rpx 30rpx;

		text {
			color: #ff3334;
			font-size: 28rpx;
		}
	}
</style>
